package io.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

/**
 * 文件字节缓冲流
 * @author Administrator
 *
 */
public class BufferedByteIODemo {

	/**
	 * 文件字节缓冲输入流：缓冲流本身不具备读写功能，所以需要传入真正具备读写功能的底层流（文件字节流）
	 * @throws IOException 
	 * 	
	 */
	@Test
	public void demo() throws IOException {
		
		// 创建
		BufferedInputStream in = new BufferedInputStream(new FileInputStream("/Users/wangzhipeng/Desktop/a.txt")); // 默认是8192字节
//		BufferedInputStream in = new BufferedInputStream(in, size); // in:底层流		size:缓冲区域的大小
		
		// 读取
		int content = in.read();
		
//		in.read(b);
		
//		in.read(b, off, len)
		
		// 关闭
		in.close();
	}
	
	/**
	 * 字节缓冲输出流
	 * @throws IOException 
	 */
	@Test
	public void demo2() throws IOException {
		
		// 缓冲流的创建
		BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("F:\\a.txt"));
//		BufferedOutputStream out = new BufferedOutputStream(out, size); // 带有缓存大小
		
		// 写入
		out.write(65);
		
//		out.write(byte[]);

//		out.write(b, off, len); // 常用
		
		// 清除缓存，清空缓存并将缓存中的内容输出到指定文件中去
		out.flush(); 
		
		// 关闭流
		out.close();
	}
	
	
	/**
	 * 文件复制案例
	 * @throws IOException 
	 */
	@Test
	public void demo3() throws IOException {
		// 创建缓冲流
		BufferedInputStream in = new BufferedInputStream(new FileInputStream("G:\\a.txt"));
		BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("F:\\aa.txt"));
		
		long timeStart = System.currentTimeMillis();
		// 复制
		int content = in.read();
		while(content != -1) {
			out.write(content);
			content = in.read();
		}
		long timeEnd = System.currentTimeMillis();
		System.out.println("复制花费的时间：" + (timeEnd - timeStart));
		
		// 关闭资源ff
		in.close();
		out.close();
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
